﻿/*
 * C/D
 *  324.2uF  0.0628  
     324.9uF  0.0694  
    -10.816uF  -0.9961  
    - 3.379pF  -0.0615 
Z/ESR
660.9m?  653.4m?
 52.03K?  47.10K?
 33.56M?  27.66M?
 34.72M?  27.66M?
 33.60M?  27.77M?
 34.62M?  28.06M?
 34.62K?  28.06K?
 34.62 ?  28.06K?
 * 
 * */
using GKQC.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;

namespace GKQC.Machine
{
    public class LcrMeter6021:ILcrMeter
    {
        public bool Check(SerialPort sp)
        {
            bool flag = false;
            return flag;
        }

        public void SetFreq(SerialPort sp, string freq, string freq2,string testItem)
        {
            if (sp.IsOpen)
            {;
                byte[] SendBytes = null;
                if (string.IsNullOrEmpty(freq)) freq = "1";
                string SendData = string.Empty;
                //if(testItem == "CD")
                {
                    SendData = "MAIN:FREQ " + freq + "\r\n";
                }
                //else
                //{
                //    SendData = "MAIN:FREQ " + freq2 + "\r\n";
                //}
                SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
                sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
            }
        }
        public void Fetch(SerialPort sp, string testItem)
        {
            if (sp.IsOpen)
            {
                byte[] SendBytes = null;
                string SendData = "MAIN:FUNC7\r\n";

                switch (testItem)
                {
                    case "CD"://"CAP和DF";
                        SendData = "MAIN:FUNC1\r\n";
                        break;
                    case "R"://"ESR";
                        SendData = "MAIN:FUNC7\r\n";
                        break;
                    case "Z"://"Z";
                        SendData = "MAIN:FUNC7\r\n";
                        break;
                    case "ZR"://"Z/ESR";
                        SendData = "MAIN:FUNC7\r\n";
                        break;
                }
                SendData += "*TRG\r\n";
                SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
                sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
            }
        }
        public string GetData(SerialPort sp)
        {
            try
            {
                byte[] readBuffer = new byte[sp.BytesToRead];
                sp.Read(readBuffer, 0, readBuffer.Length);
                return System.Text.Encoding.ASCII.GetString(readBuffer);
            }
            catch (IOException IOEx)
            {
                Logger.LogError(IOEx);
            }
            catch (InvalidOperationException IVOEx)
            {
                Logger.LogError(IVOEx);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
            return "";
        }

        public bool AnalysisData(string str, out decimal cap, out decimal df, out decimal z, out decimal r, string testItem
            ,string zrfreq,bool isRow)
        {
            cap = df = z = r = 0m; //初始化传出参数
            decimal times1 = 1m;
            decimal times2 = 1m;
            string strData = string.Empty;
            try
            {
                string[] strLines = str.Split(new[] { "\r\n" }, StringSplitOptions.None);
                if (strLines == null || strLines.Length== 0) return false;
                for (int i= strLines.Length-1;i>=0;i--)
                {
                    if (!string.IsNullOrWhiteSpace(strLines[i]))
                    {
                        strData = strLines[i];
                        break;
                    }
                }

                string[] strDatas = strData.Trim().Split(new[] { "  " }, StringSplitOptions.None);
                if (strDatas == null || strDatas.Length < 2) return false;

                //Logger.LogMessage(testItem + ":" + str); //todo:debug

                string data1 = strDatas[0].Trim();
                string data2 = strDatas[1].Trim();

                switch (testItem)
                {
                    case "CD":
                        if (data1.Contains("pF")) times1 = 0.000001m;
                        else if (data1.Contains("nF")) times1 = 0.001m;
                        else if (data1.Contains("mF")) times1 = 1000m;
                        else if (data1.Contains(" F")) times1 = 1000000m;
                        decimal.TryParse(ReplaceLetter(data1), out cap);
                        decimal.TryParse(ReplaceLetter(data2), out df);
                        times2 = 100m;
                        break;
                    case "R":
                        if (data2.Contains("p")) times2 = 0.001m;
                        else if (data2.Contains("n")) times2 = 0.000001m;
                        else if (data2.Contains(" ?")) times2 = 1000m;
                        else if (data2.Contains("K")) times2 = 1000000m;
                        else if (data2.Contains("M")) times2 = 1000000000m;
                        decimal.TryParse(ReplaceLetter(data2), out r);
                        break;
                    case "Z":
                        if (data1.Contains("p")) times1 = 0.001m;
                        else if (data1.Contains("n")) times1 = 0.000001m;
                        else if (data1.Contains(" ?")) times1 = 1000m;
                        else if (data1.Contains("K")) times1 = 1000000m;
                        else if (data1.Contains("M")) times1 = 1000000000m;
                        decimal.TryParse(ReplaceLetter(data1), out z);
                        break;
                    case "ZR":
                        if (data1.Contains("p")) times1 = 0.001m;
                        else if (data1.Contains("n")) times1 = 0.000001m;
                        else if (data1.Contains(" ?")) times1 = 1000m;
                        else if (data1.Contains("K")) times1 = 1000000m;
                        else if (data1.Contains("M")) times1 = 1000000000m;

                        if (data2.Contains("p")) times2 = 0.001m;
                        else if (data2.Contains("n")) times2 = 0.000001m;
                        else if (data2.Contains(" ?")) times2 = 1000m;
                        else if (data2.Contains("K")) times2 = 1000000m;
                        else if (data2.Contains("M")) times2 = 1000000000m;
                        decimal.TryParse(ReplaceLetter(data1), out z);
                        decimal.TryParse(ReplaceLetter(data2), out r);
                        break;
                    default:
                        break;
                }

                cap = Math.Round(cap * times1,3);
                df = Math.Round(df * times2,2);
                z = Math.Round(z * times1, 3);
                r = Math.Round(r * times2, 3);
                return true;
            }
            catch (System.Exception ex)
            {
                Logger.LogError(ex);
                return false;
            }
        }

        string ReplaceLetter(string s)
        {
            return s.Trim().Replace(" ",string.Empty)
                .Replace("F", string.Empty)
                .Replace("?", string.Empty)
                .Replace("p", string.Empty)
                .Replace("u", string.Empty)
                .Replace("m", string.Empty)
                .Replace("K", string.Empty)
                .Replace("M", string.Empty)
                .Replace("H", string.Empty)
                .Replace("n", string.Empty)
                ;
        }

        public void Open(SerialPort sp)
        {
            //ZERO:OPEN0
            //ZERO:OPEN1
            if (sp.IsOpen)
            {
                byte[] SendBytes = null;
                string SendData = "ZERO:OPEN1" + Environment.NewLine;
                SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
                sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
            }
        }

        public void Short(SerialPort sp)
        {
            //ZERO:SHORT0
            //ZERO:SHORT1
            if (sp.IsOpen)
            {
                byte[] SendBytes = null;
                string SendData = "ZERO:SHORT1" + Environment.NewLine;
                SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
                sp.Write(SendBytes, 0, SendBytes.Length);
                //发送数据 
            }
        }

        public void Remote(SerialPort sp)
        {
            if (sp.IsOpen)
            {
                byte[] SendBytes = null;
                string SendData = "REMOTE" + Environment.NewLine;
                SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
                sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
                Thread.Sleep(300);
            }
        }

        public void Local(SerialPort sp)
        {
            if (sp.IsOpen)
            {
                BackgroundWorker worker = new BackgroundWorker();
                worker.DoWork += (s, a) =>
                {
                    byte[] SendBytes = null;
                    string SendData = "LOCAL" + Environment.NewLine;
                    SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
                    sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
                };
                worker.RunWorkerAsync();
            }
        }



    }
}
